<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\index\controller;
use app\admin\logic\Log;
use app\common\model\PeopleRegister;
use app\common\model\TypeMajor;
use Elastica\Client;
use Elastica\Query\BoolQuery;
use Elastica\QueryBuilder;
use Elastica\Search;
use Elastica\Type\Mapping;
use think\Cache;
use think\Db;
use think\db\Query;
/**
 * 前端首页控制器
 */
class Index extends IndexBase
{

    public $client ;
    public $search ;
    public $zhj_index;
    public function __construct()
    {
        vendor('zhj_es.Index');
        vendor('zhj_es.jianzhu.Index');
        vendor('zhj_es.jianzhu.ConstName');
        $config = $_config = [
            'host' => '47.92.205.189',
            'port' => '9920',
        ];
        $this->client = new Client($config);
        $this->zhj_index = new \zhj_es\Index($this->client);
        $this->jianzhu = new \zhj_es\jianzhu\Index($this->client);
    }
    #新
    public function getMajor()
    {
        $this->jianzhu->getMajorByType();
    }

    public function getpeoplebyid()
    {
        ini_set("memory_limit", "2048M");
        ini_set("max_execution_time", 0);
        set_time_limit(0);
       /* $sql = "SELECT id,people_id FROM jz_people_register WHERE register_type='二级注册建造师' AND register_major = '机电工程' ORDER BY people_id DESC";
        $res = Db::query($sql);*/
       dump(Cache::get('erjijidian-jiao'));exit;
       $array1 = Cache::get('erji');
        $array2 =  Cache::get('erjijidian');
        $res = array_intersect(array_column($array1,'people_id'),array_column($array2,'people_id'));
        if($res){
            //Cache::set('erji',$res);
            Cache::set('erjijidian-jiao',$res);
            dump($res,'people_id');
        }
    }

    public function getPeopleBYregisterTYpe()
    {
        $type_num = $this->jianzhu->getType();  //获取注册类型
        $type = array_keys($type_num);
        $key = 0 ;

           while($key<17){
               $register_type = $type[$key];

               $major = $this->jianzhu->getMajorByType($register_type);
      //  var_dump($major);exit;

      //  var_dump($major);exit;
               foreach ($major as $j =>$v){
                   $sql = "SELECT id,people_id FROM jz_people_register WHERE register_type='$register_type' AND register_major ='$v' ORDER BY people_id DESC";
                   $res = Db::query($sql);
                   if($res){
                       //Cache::set('erji',$res);
                       Cache::set("$register_type--$v",$res);
                       //dump($res);
                   }else{
                       echo $j.'--'.$register_type.'<br>';
                   }
               }
             $key++;
           }


    }

    public function improtDataNew()
    {
        $key = $this->jianzhu->getTotal()+1;
        $type_num = $this->jianzhu->getType();
        $type = array_keys($type_num);
        $register_type = $type[6];
        $data = [];

            $major = $this->jianzhu->getMajorByType($register_type);

            foreach ($major as $j =>$v){
                $arr =$arr1 = [] ;
                $arr= Cache::get("$register_type--$v");
                $arr1 = array_column((array)$arr,'people_id');
                $str =  implode(',',$arr1);
                $data[$j]['id']  = $key;
                $data[$j]['register_type']  =$register_type;
                $data[$j]['register_major']  = $v;
                $data[$j]['people_id']  = $str;
                $key++;
                /*dump($str);exit;
                dump($arr1);*/
            }
        return $data;
    }

    // 测试
    public function index()
    {
        $bulidQuery = new QueryBuilder();
        $BoolQuery = new BoolQuery();
        $query = new \Elastica\Query();

        /*$bq = $BoolQuery->addMust($bulidQuery->query()->term(['title'=>'李']));
        $endQuery = $query->setQuery($bq);*/
       // echo json_encode($query->toArray());exit;

        $result1 = $this->GetSearch()->search($query);
        dd($result1->getData()) ;exit;
        $tradeList = array_map(function ($result) {
            return array_merge($result->getData(), ["highlight"=> $result->getHighlights()]);
        }, $result1->getResults());
        var_dump($tradeList);exit;
    }

    //用于检索的
    public function GetSearch()
    {

        $search = new Search($this->client);
          $search->addIndex('jianzhu')->addType('jz_people_register');
        $zhj =new \zhj_es\Index($this->client);
        $zhj->SetType();
    }
    //创建索引
    public function createIndex()
    {
        $elasticIndex = new \zhj_es\Index($this->client);
        dump($elasticIndex->createIndex());
    }
    //导入数据
    public function importData()
    {

        ini_set("memory_limit", "1024M");
        ini_set("max_execution_time", 0);
        set_time_limit(0);
        $elasticIndex = new \zhj_es\Index($this->client);

         $count =  Db::name('people_register_new')->distinct(true)->count('id');

         $page = ceil($count/1000);
         $k = 0;
         while($page){
          $companyUrlList = Db::name('people_register_new')->distinct(true)->order('id')->limit($k*1000,1000)->select();

          foreach ($companyUrlList as $k1=>&$v){
              $v['register_type_major'] = "$v[register_type]--$v[register_major]";
              $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
//              $list[$k]['company_url'] = $v['company_url'];
//              $list[$k]['people_info'] = $peopleList;

          }

          $elasticIndex->importData($companyUrlList);
            $page--;
             $k++;
            echo $page.'<br>';
        }
           //$list = $this->improtDataNew();
           //$elasticIndex->importData($list);
           \think\Log::error("run 1 page");
         //  (new Log())->logAdd('import_data',"run $page page");
           echo 1;
    }

    public function deleteIndex()
    {
       $index =  'jz_people_register';
       $res =  $this->zhj_index->deleteIndex($index);
       var_dump($res);exit;
    }
    #获取注册数据
    public function getRegisterPeople()
    {
        ini_set("memory_limit", "2048M");
        ini_set("max_execution_time", 0);
        set_time_limit(0);
        $page = 0;
        $elasticIndex = new \zhj_es\Index($this->client);
        $people_register = new PeopleRegister();
        while($page<40){
            $list = $people_register->getListData([],"*",'id desc',$page,100000);
            $elasticIndex->importData($list);
            echo $page;
            $page++;
        }
    }
    public function assembleData($page)
    {
        $people_register =new PeopleRegister() ;
        $type_major = new TypeMajor();
        //$page = 1;
        $page_size = 20;
        $company_list = $people_register->getCompanyUrl('company_url ',$page,$page_size,'company_url');
        $list = [];
        foreach ($company_list as $k=>$value){
            $registerInfo = [];
            $registerInfo =  (new PeopleRegister())->getRegister($value['company_url']);
            $list[$k]['id'] = ($page*$page_size)+$k+1;
            $list[$k]['company_url'] =$value['company_url'];
            $list[$k]['people_id'] = array_column($registerInfo,'people_id');
            $list[$k]['register_id'] = array_column($registerInfo,'id');
            $list[$k]['register_type_major'] = array_column($registerInfo,'type_major');
            $list[$k]['register_type_major_id'] = $this->getMajorIdsByName(array_column($registerInfo,'type_major'));
        }
        return $list;
    }

    public function createNewTable()
    {
        $people_register = new PeopleRegister();
        $type_major  = new TypeMajor();
        $list = $people_register->getDistinctTypeMajor();
        $data = [];
        foreach ($list as $k=>$value){
            $data[$k]['id'] = $k+1;
            $data[$k]['type_major'] = $value["CONCAT_WS('--',`register_type`,register_major)"];
        }
       // dump($data);exit;
       $res = $type_major->insetData($data);
        dump($res);exit;
    }

    public function getMajorIdsByName($data)
    {
        $ids = [];
        foreach($data as $value){
            $ids[] = TypeMajor::getIdByTypeMajor(['type_major'=>$value])[0]['id'];
        }
        return $ids;
    }
}
